﻿using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using Hollysys.Mes.ProductionManage.IRepository;

namespace Hollysys.Mes.ProductionManage.Repository
{
    public abstract class RepositoryBase<T> : IRepository<T> where T : class,new()
    {
        public DbContext context;
        /// <summary>
        /// 提供IOC注入方式接口
        /// </summary>
        /// <param name="context"></param>
        public RepositoryBase(DbContext context)
        {
            this.context = context;
        }
        /// <summary>
        /// 测试用
        /// </summary>
        public RepositoryBase()
        {
            this.context = new ProMngEntities();
        }

        #region IRepository<T> 成员

        public T Create()
        {
            return context.Set<T>().Create();
        }
        public T Update(T entity)
        {
            //执行验证业务
            //context.Entry<T>(entity).GetValidationResult(); if (context.Entry<T>(entity).State == EntityState.Modified) context.SaveChanges();
            return entity;
        }
        public T Insert(T entity)
        {
            context.Set<T>().Add(entity);
            context.SaveChanges(); 
            return entity;
        }
        public void Delete(T entity)
        {
            context.Set<T>().Remove(entity);
            context.SaveChanges();
        }
        public T Find(params object[] keyValues)
        {
            return context.Set<T>().Find(keyValues);
        }
        public List<T> FindAll()
        {
            return context.Set<T>().ToList();
        }
        #endregion IRepository<T> 成员
    }
}
